package com.um.jdy.ykc.service.impl;

import com.um.jdy.common.charge.enums.OrderEnum;
import com.um.jdy.common.device.dao.FeeEvPriceDao;
import com.um.jdy.common.device.dao.FeeEvTimeDao;
import com.um.jdy.common.device.dao.PileDao;
import com.um.jdy.common.device.entity.po.FeeEvPrice;
import com.um.jdy.common.device.entity.po.FeeEvTime;
import com.um.jdy.common.device.entity.po.Pile;
import com.um.jdy.ykc.model.PriceModel;
import com.um.jdy.ykc.service.PriceModelService;
import com.um.jdy.ykc.utils.LocalDateUtil;
import com.um.springboot.starter.utils.NumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 邓南辉
 * 日期 2023/8/10
 * 版权所有 广州优匠科技有限公司
 */
@Service
public class PriceModelServiceImpl implements PriceModelService {

    @Autowired
    FeeEvPriceDao feeEvPriceDao;

    @Autowired
    FeeEvTimeDao feeEvTimeDao;

    @Autowired
    PileDao pileDao;


    /**
     * 查询计费模型
     * @param imei
     * @return
     */
    @Override
    public PriceModel getPriceModel(String imei){
        Pile pile = pileDao.getEntity("imei",imei);
        if(pile != null){
            List<FeeEvPrice> priceList = feeEvPriceDao.getList(new String[]{"device_fee_id"}, new String[]{pile.getDeviceFeeId()},
                    new String[]{"sort_no"}, new String[]{"asc"});
            List<FeeEvTime> timeList = feeEvTimeDao.getList(new String[]{"device_fee_id"}, new String[]{pile.getDeviceFeeId()},
                    new String[]{"start_time"}, new String[]{"asc"});

            // 获取平台计费模型数据
            PriceModel model = new PriceModel();
            List<Integer> time48Fee = new ArrayList<>();

            if(timeList.size() > 0) {
                for (FeeEvTime feeEvTime : timeList) {
                    OrderEnum.ChargePeriod period = OrderEnum.ChargePeriod.valueOf(feeEvTime.getType());
                    String startTime = feeEvTime.getStartTime();
                    String endTime = feeEvTime.getEndTime();

                    long diffTime = LocalDateUtil.getDiffTime(startTime+":00",endTime+":00",  "HH:mm:ss");
                    long time = diffTime / (1000 * 60 * 30);
                    for (int index = 0; index < time; index++) {
                        time48Fee.add(period.ordinal());
                    }
                }
            }else{
                for(int index = 0;index < 48;index ++){
                    time48Fee.add(0);
                }
            }

            if(priceList.size() > 0) {
                for (FeeEvPrice feeEvPrice : priceList) {
                    String type = feeEvPrice.getType();
                    switch (OrderEnum.ChargePeriod.valueOf(type)){
                        case Sharp:
                            model.setSharpFee(NumberUtil.integerToDouble(feeEvPrice.getUnitPrice(),4));
                            model.setSharpServiceFee(NumberUtil.integerToDouble(feeEvPrice.getServiceFee(),4));
                            break;
                        case Peak:
                            model.setPeakFee(NumberUtil.integerToDouble(feeEvPrice.getUnitPrice(),4));
                            model.setPeakServiceFee(NumberUtil.integerToDouble(feeEvPrice.getServiceFee(),4));
                            break;
                        case Flat:
                            model.setShoulderFee(NumberUtil.integerToDouble(feeEvPrice.getUnitPrice(),4));
                            model.setShoulderServiceFee(NumberUtil.integerToDouble(feeEvPrice.getServiceFee(),4));
                            break;
                        case Valley:
                            model.setOffPeakFee(NumberUtil.integerToDouble(feeEvPrice.getUnitPrice(),4));
                            model.setOffPeakServiceFee(NumberUtil.integerToDouble(feeEvPrice.getServiceFee(),4));
                            break;
                    }
                }
            }else{
                model.setSharpFee(0.6000);
                model.setSharpServiceFee(0.0000);

                model.setPeakFee(0.6000);
                model.setPeakServiceFee(0.0000);

                model.setShoulderFee(0.6000);
                model.setShoulderServiceFee(0.0000);

                model.setOffPeakFee(0.6000);
                model.setOffPeakServiceFee(0.0000);
            }


            model.setFeeMode("0100");
            model.setLossScale(0);
            model.setTime48Fee(time48Fee);

            return model;
        }

        return null;

    }

}
